package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.pojos.FunctionType;
import cn.gson.springboot.model.service.FunctionTypeService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FunctionTypeController {
    @Autowired
    FunctionTypeService fts;

    @RequestMapping("/dept-funtype")
    @ResponseBody
    public Map<String,Object> deptFunType(Integer deptId){
        Map<String,Object> map = new HashMap<>();
        List<FunctionType> functionTypes = fts.allFunTypes();
        List<Integer> deptFunTypes = fts.deptFunType(deptId);
        map.put("functionTypes",functionTypes);
        map.put("deptFunTypes",deptFunTypes);
        return map;
    }
    @RequestMapping("/save-dept-jurisdiction")
    @ResponseBody
    public void saveDeptJurisdiction(@RequestParam("str") String str){
        JSONObject parse = (JSONObject) JSONObject.parse(str);
        Integer deptId = (Integer)parse.get("deptId");
        List<Integer> funTypeId = (List<Integer>)parse.get("funTypeId");
        fts.saveDeptJurisdiction(deptId,funTypeId);
    }

    @RequestMapping("/fun-menu")
    @ResponseBody
    public List<FunctionType> funMenu(HttpSession session){
        Employee e = (Employee) session.getAttribute("emp");
        if(e != null){
            return fts.funMenu(e.getEmpId());
        }
        return null;
    }

    @RequestMapping("/job-funType")
    @ResponseBody
    public Map<String,Object> jobFunType(Integer jobId){
        Map<String,Object> map = new HashMap<>();
        List<Integer> jobFuns = fts.jobFuns(jobId);
        List<FunctionType> functionTypes = fts.jobFunType(jobId);
        map.put("jobFuns", jobFuns);
        map.put("functionTypes",functionTypes);
        return map;
    }
}
